#include "fireman.h"
#include "dijkstra.h"
#include "Utils.h"

FireMan::FireMan(){
}
FireMan::FireMan(int id){
    this->id=id;
}

/*
 * 由起点和终点计算出两点间的最短路径（适用于起点和终点在非路口节点）
 *from:起点坐标
 *to:终点坐标
 *return    返回最短路径
*/
QVector<QVector<double>> FireMan::finalPath(QVector<double> from, QVector<double> to){
    Dijkstra finder;
    QVector<QVector<double>> PathPoint;//记录路口节点
    QVector<QVector<double>> FinalPoint;//记录最终路径上的点
    // 构建示例图
    finder.Init();
    int from_num=finder.addPoint(from);
    //int to_num=finder.addPoint(to);
    auto result = finder.calculateShortestPath(from_num, 3);

    if (result.isValid) {
        qDebug() << "最短路径长度:" << result.totalDistance;
        qDebug() << "路径:" << result.path;
    } else {
        qDebug() << "路径不存在";
    }
    QVector<QVector<double>> path;
    for (auto point: result.path){
        PathPoint.append(DataBase::getInstance().getPointData(point));
    }


    PathPoint[0]=from;
    if(PathPoint.size()>1){
        double d=sqrt(pow(PathPoint[0][0]-PathPoint[1][0],2)+pow(PathPoint[0][2]-PathPoint[1][2],2));
        if(d<=25){
            //qDebug()<<PathPoint[0][0]<<"    "<<PathPoint[1][0]<<"   "<<"     "<<PathPoint[0][1]<<"    "<<PathPoint[1][1]<<"d:   "<<d;
            PathPoint[1]=PathPoint[0];
        }
    }

    qDebug()<<"最短路径为："<<PathPoint;
    FinalPoint=interpolatePointsSequence(PathPoint,5.0);
    // info["number"]=QString::number(id);
    // info["distance"]=QString::number(result.totalDistance);
    // info["totaldistance"]=info["distance"].toDouble()>info["totaldistance"].toDouble()?info["distance"]:info["totaldistance"];
    // QList<QString> temp=calInfo(info);
    // info["time"]=temp[0];
    // info["juli"]=temp[1];
    //DataBase::getInstance().handerData("fireman_information",info,"number");
    DataBase::getInstance().clearData("fireman1_savepath");
    writePath(FinalPoint);
    return FinalPoint;
}

/*
 * 由起点和终点计算出两点间的最短路径（适用于简单迪杰斯特拉）
 *from:起点编号
 *to:终点编号
 *return    返回最短路径
*/
QVector<QVector<double>> FireMan::finalPath(int from, int to){
    Dijkstra finder;
    QVector<QVector<double>> PathPoint;//记录路口节点
    QVector<QVector<double>> FinalPoint;//记录最终路径上的点
    // 构建示例图
    finder.Init();
    auto result = finder.calculateShortestPath(from, to);

    if (result.isValid) {
        qDebug() << "最短路径长度:" << result.totalDistance;
        qDebug() << "路径:" << result.path;
    } else {
        qDebug() << "路径不存在";
    }

    QVector<QVector<double>> path;
    for (auto point: result.path){
        PathPoint.append(DataBase::getInstance().getPointData(point));
    }
    qDebug()<<"最短路径为："<<PathPoint;
    FinalPoint=interpolatePointsSequence(PathPoint,5.0);

    // info["number"]=QString::number(id);
    // info["distance"]=QString::number(result.totalDistance);
    // info["totaldistance"]=info["distance"].toDouble()>info["totaldistance"].toDouble()?info["distance"]:info["totaldistance"];
    // QList<QString> temp=calInfo(info);
    // info["time"]=temp[0];
    // info["juli"]=temp[1];
    //DataBase::getInstance().handerData("fireman_information",info,"number");
    DataBase::getInstance().clearData("fireman1_savepath");
    writePath(FinalPoint);
    return FinalPoint;
}

